#include <stdio.h>


extern char * simple_strchr(const char *s, int c);

char *
simple_strrchr (const char *s, int c)
{
	const char *ret = NULL;
	
	for (; *s != '\0'; ++s)
		if (*s == (char) c)
			ret = s;
		
		return (char *) (c == '\0' ? s : ret);
}


static int  ch = 'r';

static char string[] = "The quick brown dog jumps over the lazy fox";
static char fmt1[] =   "         1         2         3         4         5";
static char fmt2[] =   "12345678901234567890123456789012345678901234567890";

static void _main( void )
{
	char *pdest;
	int result;
	
	printf( "String to be searched: \n\t\t%s\n", string );
	printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
	printf( "Search char:\t%c\n", ch );
	
	/* Search forward. */
	pdest = simple_strchr( string, ch );
	result = pdest - string + 1;
	if( pdest != NULL )
		printf( "Result:\tfirst %c found at position %d\n\n", 
		ch, result );
	else
		printf( "Result:\t%c not found\n" );
	
	/* Search backward. */
	pdest = simple_strrchr( string, ch );
	result = pdest - string + 1;
	if( pdest != NULL )
		printf( "Result:\tlast %c found at position %d\n\n", ch, result );
	else
		printf( "Result:\t%c not found\n" );
}